package com.example.demo.service.impl;

import com.example.demo.dto.ApplicationDTO;
import com.example.demo.entity.Application;
import com.example.demo.mapper.ApplicationMapper;
import com.example.demo.request.CreateApplicationRequest;
import com.example.demo.service.ApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.exception.BusinessException;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ApplicationServiceImpl implements ApplicationService {

    @Autowired
    private ApplicationMapper applicationMapper;

    @Override
    public List<ApplicationDTO> getApplications(int page, int size) {
        // 实际开发中应该使用分页查询
        List<Application> applications = applicationMapper.selectAll();
        return applications.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    @Override
    public long countApplications() {
        return applicationMapper.count();
    }

    @Override
    public ApplicationDTO getApplicationById(Integer id) {
        Application application = applicationMapper.selectById(id);
        return convertToDTO(application);
    }

    @Override
    public Application createApplication(CreateApplicationRequest request) {
        // 检查应用名称是否重复
        Application existingByName = applicationMapper.selectByAppName(request.getAppName());
        if (existingByName != null) {
            throw new BusinessException("应用名称'" + request.getAppName() + "'已存在");
        }
        
        // 检查应用英文名是否重复
        Application existingByEnName = applicationMapper.selectByAppEnName(request.getAppEnName());
        if (existingByEnName != null) {
            throw new BusinessException("应用英文名'" + request.getAppEnName() + "'已存在");
        }
        
        Application application = new Application();
        application.setAppName(request.getAppName());
        application.setAppEnName(request.getAppEnName());
        application.setAppDesc(request.getAppDesc());
        application.setDeptId(request.getDeptId());
        application.setDeptName(request.getDeptName());
        applicationMapper.insert(application);
        return application;
    }

    @Override
    public void updateApplication(Integer id, CreateApplicationRequest request) {
        // 检查应用名称是否重复（排除当前应用）
        Application existingByName = applicationMapper.selectByAppNameExcludeId(request.getAppName(), id);
        if (existingByName != null) {
            throw new BusinessException("应用名称'" + request.getAppName() + "'已存在");
        }
        
        // 检查应用英文名是否重复（排除当前应用）
        Application existingByEnName = applicationMapper.selectByAppEnNameExcludeId(request.getAppEnName(), id);
        if (existingByEnName != null) {
            throw new BusinessException("应用英文名'" + request.getAppEnName() + "'已存在");
        }
        
        Application application = applicationMapper.selectById(id);
        application.setAppName(request.getAppName());
        application.setAppEnName(request.getAppEnName());
        application.setAppDesc(request.getAppDesc());
        application.setDeptId(request.getDeptId());
        application.setDeptName(request.getDeptName());
        applicationMapper.updateById(application);
    }

    @Override
    public void deleteApplication(Integer id) {
        applicationMapper.deleteById(id);
    }

    @Override
    public List<ApplicationDTO> searchApplicationsByName(String name) {
        List<Application> applications = applicationMapper.selectByNameLike(name);
        return applications.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    private ApplicationDTO convertToDTO(Application application) {
        ApplicationDTO dto = new ApplicationDTO();
        dto.setAppId(application.getAppId());
        dto.setAppName(application.getAppName());
        dto.setAppEnName(application.getAppEnName());
        dto.setAppDesc(application.getAppDesc());
        dto.setDeptId(application.getDeptId());
        dto.setDeptName(application.getDeptName());
        return dto;
    }
}